package com.ressource;

import java.io.File;
import java.net.URL;
import java.text.MessageFormat;
import java.util.Locale;
import java.util.ResourceBundle;

/**
 * This class manages locales, defined for each supported language in a
 * properties file (in the source code, {@code
 * src/resource/locale/locale*.properties}).
 */

public class LocaleManager implements InterfaceResourceManager
{
	/**
	 * The prefix of each locale file.
	 */

	private static final String			LOCALE_PROPERTIES_PREFIX	= "resource"
																			+ File.separator
																			+ "locale"
																			+ File.separator
																			+ "locale";

	/**
	 * The resource bundle defined from the properties files chosen by the the
	 * system according to the system locale.
	 */
	private static final ResourceBundle	LOCALE_BUNDLE				= ResourceBundle
																			.getBundle(
																					LOCALE_PROPERTIES_PREFIX,
																					Locale
																							.getDefault());

	/**
	 * Forbids instantiation of this class.
	 */
	private LocaleManager()
	{

	}

	/**
	 * Returns a localized string described by its key in the properties file.
	 * 
	 * @param key
	 *            the key of the message to be translated
	 * @param arguments
	 *            a set of parameters that are dynamically set in the localized
	 *            string
	 * @return the localized string described by the {@code key} key
	 */
	public static String getLocalizedString(String key, String... arguments)
	{
		MessageFormat mf = new MessageFormat("");
		mf.applyPattern(LOCALE_BUNDLE.getString(key));

		return mf.format(arguments);
	}

	/**
	 * Returns the correct path (as an URL) to the main page of the localized
	 * HTML documentation according to the current locale. For a given locale
	 * <i>lc</i>, the corresponding documentation is in {@code
	 * doc_lc/JTeXGraphe_lc.html}.
	 * 
	 * @return the local URL to the main page of the localized HTML
	 *         documentation.
	 */
	public static String getLocalizedHelpURL()
	{
		URL u = ClassLoader.getSystemResource(LOCALE_BUNDLE
				.getString("HelpPath"));

		return u.toExternalForm();
	}
}
